package com.sinopec.android.util;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.security.MessageDigest;

import android.content.Context;


/**
 * @ClassName:StringUtil
 * @Description:对字符串进行处理的类
 * @author liuwei
 * @date 2011-6-7下午04:49:18
 *
 */
public class StringUtil {
	/**
	 * 判断给定字符串是否空白串。<br>
	 * 空白串是指由空格、制表符、回车符、换行符组成的字符串<br>
	 * 若输入字符串为null或空字符串，返回true
	 * @param input
	 * @return boolean
	 */
	public static boolean isEmpty( String input ) 
	{
		if ( input == null || "".equals( input ) )
			return true;
		
		for ( int i = 0; i < input.length(); i++ ) 
		{
			char c = input.charAt( i );
			if ( c != ' ' && c != '\t' && c != '\r' && c != '\n' )
			{
				return false;
			}
		}
		return true;
	}
	
	public static boolean isNotEmpty(String s) {
		return (s != null && s.trim().length() > 0);
	}
	
	public static String readAssetsCity(Context context,String fileName) {
		InputStream is = null;
		StringBuilder sb = new StringBuilder();
		if(fileName == null || fileName.equals("")){
			return null;
		}
		try {
			is = context.getAssets().open(fileName);
			BufferedReader br = new BufferedReader(new InputStreamReader(is,"GBK")); //创建读取流
			
			sb.append(br.readLine());
		
			String line ;
			while((line = br.readLine())!=null){
//				temp+=line;
				sb.append(line);
			}
		} catch (Exception e) {
			return null;
		}
		return sb.toString();
	}
	
	public static String beijingStr ; 
	public static String shanghaiStr ; 
	public static String guangzhouStr ; 
	
	public static String readFromFile(Context context,String fileName) {
		InputStream is = null;
		StringBuilder sb = new StringBuilder();
		
//		String temp = null;
		File file;
		if(fileName == null || fileName.equals("")){
			return null;
		}
		try {
			file = context.getFileStreamPath(fileName);
			is = new FileInputStream(file);
			InputStreamReader isr = new InputStreamReader(is,"gbk");
			int c;
			char[] charStr = new char[1024];
			while ((c = isr.read(charStr)) != -1) {
				sb.append(charStr,0,c);
			}
			
			is.close();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		if(fileName.equals("beijing.txt")){
			beijingStr = sb.toString();
			return beijingStr;
		}else if(fileName.equals("shanghai.txt")){
			shanghaiStr = sb.toString();
			return shanghaiStr;
		}else if(fileName.equals("guangzhou.txt")){
			guangzhouStr = sb.toString();
			return guangzhouStr;
		}else{
			return null;
		}
	}
	
	public static void writeCityToFile(Context context,String fileName) {
		InputStream is = null;
		if(fileName == null || fileName.equals("")){
			return ;
		}
		try {
//			ew
			is = context.getAssets().open(fileName);
			File file = context.getFileStreamPath(fileName);
			FileOutputStream output = new FileOutputStream(file);
	         	byte[] buffer = new byte[4096];
	             int n = 0;
	             while (-1 != (n = is.read(buffer))) {
	                 output.write(buffer, 0, n);
	             }
	       output.close();
	       is.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void writeNewCityToFile(Context context,String fileName,String str){
		File file = context.getFileStreamPath(fileName);
		FileOutputStream fos=null;
		try {
			fos = new FileOutputStream(file);
			OutputStreamWriter osw=new OutputStreamWriter(fos);
			osw.write(str);
			osw.close();
			fos.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public  static String MD5(String s) {
		 
		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
		 
		'a', 'b', 'c', 'd', 'e', 'f' };
		 
		try {
		 
		byte[] strTemp = s.getBytes();
		 
		//使用MD5创建MessageDigest对象
		 
		MessageDigest mdTemp = MessageDigest.getInstance("MD5");
		 
		mdTemp.update(strTemp);
		 
		byte[] md = mdTemp.digest();
		 
		int j = md.length;
		 
		char str[] = new char[j * 2];
		 
		int k = 0;
		 
		for (int i = 0; i < j; i++) {
		 
		byte b = md[i];
		 
		//System.out.println((int)b);
		 
		//将没个数(int)b进行双字节加密
		 
		str[k++] = hexDigits[b >> 4 & 0xf];
		 
		str[k++] = hexDigits[b & 0xf];
		 
		}
		 
		return new String(str);
		 
		} catch (Exception e) {return null;}
		 
		}
}
